<?php
	/*
		@处理后台管理
	*/
	session_start();	//开启session
	
	@$action = $_GET['action'];
	//处理方法
	if(empty($action) || !isset($action)){$action = "default";}

	if($action == "default"){
		if(empty($_SESSION['username']) || !isset($_SESSION['sid'])){
			header("Location:index.php?controller=admin&action=login");
			exit;
		}else{
			//已经登录
			$smarty->assign('title','先锋视频后台管理');
			$smarty->display('admin/admin.tpl');
		}
	}else if($action == "login"){
		$smarty->assign('title','登录');
		$smarty->display('admin/login.tpl');
	}else if($action == "login_action"){
		$username = $_POST['username'];
		$password = $_POST['password'];
		
		include_once("configs/database.inc.php");		//引入数据库配置

		if($D->connect()){
			if($D->rows($D->query("SELECT * FROM `admin` WHERE username = '".$username."' AND PASSWORD = md5( '".$password."' )"))){
				$_SESSION['username'] = $username;
				$_SESSION['sid'] = session_id();
				header("Location:index.php?controller=admin&action=default");
				exit;
			}else{
				echo "<script>alert(\"非法用户！\");location.href=\"index.php?controller=admin&action=login\";</script>";
			}
		}else{
				echo "<script>alert(\"登录失败！\");location.href=\"index.php?controller=admin&action=login\";</script>";
		}
	}else if($action == "sysinfo"){
	/*
	 *	功能：关于系统信息
	 *	日期：2010-04-29
	*/
		require_once("applications/sysinfo.php");
	}else if($action == "about"){
	/*
	 *	功能：系统信息展示
	 *	日期：2010-04-29
	*/
		$smarty->assign('title','关于系统');
		$smarty->display('admin/about.tpl');
	}else if($action == "notice"){
	/*
	 *	功能：通知管理模块
	 *  日期：2010-04-28
	*/
		include_once("configs/database.inc.php");		//引入数据库配置
		$D->query("set names utf8");
		if($D->rowsAll("notice")){
			$res_notice = $D->fetchAll("notice");
		}else{
			$res_notice = "NULL";	
		}
		$smarty->assign('title','通知列表');
		$smarty->assign('res_notice',$res_notice);
		$smarty->display('admin/notice.tpl');
	}else if($action == "addNotice"){
	/*
	 *	功能：发布通知模块
	 *  日期：2010-04-28
	*/
		$smarty->assign('title','发布通知');
		$smarty->display('admin/addNotice.tpl');
	}else if($action == "addNoticeAction"){
	/*
	 *	功能：发布通知动作处理
	 *  日期：2010-04-28
	*/
		$title = $_POST['title'];
		@$hot = $_POST['hot'];
		if(empty($hot) || !isset($hot)){$hot = "hot-no";}
		$content = $_POST['content'];
		if(empty($title) || empty($content)){
			header("Location:index.php?controller=admin&action=addNotice");
			exit;
		}
		include_once("configs/database.inc.php");		//引入数据库配置
		$D->query("set names utf8");
		if($D->rows($D->query("SELECT * FROM `notice` WHERE `title` like '".$title."'"))){
			echo "<script>alert(\"标题已经存在！\");location.href=\"index.php?controller=admin&action=addNotice\";</script>";	
			exit;
		}
		$data = array(
					  	"title" 	=>  $title,
						"content"	=>  $content,
						"hot"		=>  $hot,
						"postdate"=>  date("Y-m-d H:i")
					  );
		if($D->insert("notice",$data)){
				echo "<script>alert(\"通知添加成功！\");location.href=\"index.php?controller=admin&action=addNotice\";</script>";
				exit;
		}
	}else if($action == "delNotice"){
	/*
	 *	功能：删除通知
	 *  日期：2010-04-28
	*/
		$id = $_GET['id'];
		if(empty($id) || !isset($id)){
			header("Location:index.php?controller=admin&action=notice");	
			exit;
		}
		include_once("configs/database.inc.php");		//引入数据库配置
		if($D->delete("notice",array("id" => $id))){
			echo "<script>alert(\"删除通知成功！\");location.href=\"index.php?controller=admin&action=notice\";</script>";
			exit;
		}else{
			echo "<script>alert(\"删除通知失败！\");location.href=\"index.php?controller=admin&action=notice\";</script>";	
			exit;
		}
	
	}else if($action == "live"){
	/*
	 *	功能：直播管理
	 *	日期：2010-05-07
	*/
		include_once("configs/database.inc.php");	
		$D->query("set names utf8");	
		$res_live = $D->fetchAll("live");
		$smarty->assign('title','直播管理');
		$smarty->assign('res_live',$res_live);
		$smarty->display('admin/live.tpl');
	}else if($action == "editLive"){
	/*
	 *	功能：编辑直播内容
	 *	日期：2010-05-08
	*/	
		$id = $_GET['id'];
		if(empty($id) || !isset($id)){
			header("Location:index.php?controller=admin&action=live");	
			exit;
		}
		include_once("configs/database.inc.php");	
		$D->query("set names utf8");	
		$res_live_where_sets = $D->get_where("live",array("id" => $id));
		$res_live_where = $res_live_where_sets[0];

		$smarty->assign('id',$id);
		$smarty->assign('res_live_where',$res_live_where);
		$smarty->assign('title','编辑频道');
		$smarty->display('admin/editLive.tpl');
	}else if($action == "editLiveAction"){
	/*
	 *	功能：编辑直播内容动作处理
	 *	日期：2010-05-08
	*/
		$id = $_POST['id'];
		if(empty($id) || !isset($id)){
			header("Location:index.php?controller=admin&action=live");	
			exit;
		}
		include_once("configs/database.inc.php");		//引入数据库配置
		$D->query("set names utf8");
		$content = $_POST['content'];
		$open_state = $_POST['open_state'];
		
		//进行数据更新
		$data_array = array("content" => $content,"open_state" => $open_state);
		$where_array = array("id" => $id);
		if($D->update("live",$data_array,$where_array)){
			echo "<script>alert(\"频道编辑成功！\");location.href=\"index.php?controller=admin&action=live\";</script>";	
		}else{
			echo "<script>alert(\"频道编辑出错！\");location.href=\"index.php?controller=admin&action=live\";</script>";	
		}		
	}else if($action == "broadcast"){
	/*
	 *	功能：点播管理
	 *	日期：2010-05-02
	*/
		include_once("configs/database.inc.php");		//引入数据库配置
		$D->query("set names utf8");
		if($D->rowsAll("broadcast")){
			$res_broadcast = $D->fetchAll("broadcast");
		}else{
			$res_broadcast = "NULL";	
		}
				
		$smarty->assign('title',"点播管理");
		$smarty->assign('res_broadcast',$res_broadcast);
		$smarty->display('admin/broadcast.tpl');
	}else if($action == "addMedia"){
	/*
	 *	功能：添加视频功能
	 *	日期：2010-05-02
	*/
		include_once("configs/database.inc.php");	
		$D->query("set names utf8");	
		if($D->rowsAll("broadcastCategory")){
			$res_broadcastCategory = $D->fetchAll("broadcastCategory");
		}else{
			$res_broadcastCategory = "NULL";	
		}

		$smarty->assign('title',"添加视频");
		$smarty->assign('res_broadcastCategory',$res_broadcastCategory);
		$smarty->display('admin/addMedia.tpl');
	}else if($action == "addMediaAction"){
	/*
	 *	功能：处理文件上传动作
	 *	日期：2010-05-05
	*/
		include_once("libraries/Uploader.class.php");
		$upDir = 'uploads/'.date('Y-m-d');
		$uploader = new Uploader('uploadfile');
		$uploader->setValidExt(array('.jpg','.gif','.avi','.rmvb'));
		$uploader->setMaxSize(1024*1000*1000);	//1024*1000=1M，最大为1G
		$uploader->setUpDir($upDir); 		//需修改为你自己的路径
		if(!$uploader->upload()){
			echo "<script>alert(\"".$uploader->getErrorMsg()."！\");location.href=\"index.php?controller=admin&action=addMedia\";</script>";	
		}else{
			echo "<script>alert(\"文件上传成功！\");</script>";	
			
			include_once("configs/database.inc.php");	
			$D->query("set names utf8");	
			$res_broadcastCategory = $D->fetchAll("broadcastCategory");
			$urlPath = $uploader->getFilePath(); 
			$smarty->assign('urlPath',$urlPath);
			$smarty->assign('res_broadcastCategory',$res_broadcastCategory);
			$smarty->display('admin/addBroadcast.tpl');
		}

	}else if($action == "addBroadcastAction"){
	/*
	 *	功能：添加视频功能动作处理
	 *	日期：2010-05-02
	*/
		$category = $_POST['category'];
		$title = $_POST['title'];
		$content = $_POST['content'];
		$souce = $_POST['souce'];
		$urlPath = $_POST['urlPath'];
		include_once("configs/database.inc.php");	
		$D->query("set names utf8");
		$data = array(
					  	'category'	=>	$category,
						'title'		=>	$title,
						'content'	=>	$content,
						'souce'		=>	$souce,
						'urlPath'	=>	$urlPath,
						'uploadtime'=>	date('Y-m-d H:i')
					  );
		if($D->insert("broadcast",$data)){
			echo "<script>alert(\"视频添加成功！\");location.href=\"index.php?controller=admin&action=addMedia\";</script>";
			exit;
		}else{
			//视频添加失败，要执行回滚操作，这一块没有测试到，可能存在Bug
			echo "<script>alert(\"视频添加失败，正在执行回滚……\");</script>";
			if(unlink($urlPath)){
				echo "<script>alert(\"回滚成功！\");</script>";	
			}else{
				echo "<script>alert(\"回滚操作失败，请手动删除相关文件！\");</script>";	
			}
			header("Location:index.php?controller=admin&action=addMedia");
			exit;
		}
	}else if($action == "showBroadcast"){
	/*
	 *	功能：显示视频详细信息
	 *	日期：2010-05-07
	*/
		$id = $_GET['id'];
		if(empty($id) || !isset($id)){
			header("Location:index.php?controller=admin&action=broadcast");	
			exit;
		}
		include_once("configs/database.inc.php");	
		$D->query("set names utf8");	
		$res_broadcast_where_sets = $D->get_where("broadcast",array("id" => $id));
		$res_broadcast_where = $res_broadcast_where_sets[0];
		$smarty->assign('title','视频详细信息');
		$smarty->assign('res_broadcast_where',$res_broadcast_where);
		$smarty->display('admin/showBroadcast.tpl');
	}else if($action == "delBroadcast"){
	/*
	 *	功能：删除视频信息
	 *	日期：2010-05-07
	*/
		$id = $_GET['id'];
		if(empty($id) || !isset($id)){
			header("Location:index.php?controller=admin&action=broadcast");	
			exit;
		}
		include_once("configs/database.inc.php");		//引入数据库配置
		$D->query("set names utf8");
		//查到视频的路径文件位置
		$urlPath_sets  = $D->get_where("broadcast",array("id" => $id));
		$urlPath = $urlPath_sets[0]['urlPath'];
		if($D->delete("broadcast",array("id" => $id)) && @unlink($urlPath)){
			$pathDir = substr($urlPath,0,18);
			include_once("libraries/Functions.inc.php");
			if(is_empty_dir($pathDir)){
				rmdir($pathDir);
			}
			echo "<script>alert(\"删除视频成功！\");location.href=\"index.php?controller=admin&action=broadcast\";</script>";
			exit;
		}else{
			echo "<script>alert(\"删除视频失败！\");location.href=\"index.php?controller=admin&action=broadcast\";</script>";	
			exit;
		}
	}else if($action == "addBroadcastCategory"){
	/*
	 *	功能：添加视频类别功能
	 *	日期：2010-05-02
	*/
		$smarty->assign('title',"添加视频类别");
		$smarty->display('admin/addBroadcastCategory.tpl');
	}else if($action == "addBroadcastCategoryAction"){
	/*
	 *	功能：添加视频类别动作处理
	 *	日期：2010-05-03
	*/
		$categoryName = $_POST['categoryName'];
		if(empty($categoryName)){
			header("Location:index.php?controller=admin&action=addBroadcastCategory");
			exit;
		}
		
		include_once("configs/database.inc.php");	
		$D->query("set names utf8");		
		if($D->rows($D->query("SELECT * FROM `broadcastCategory` WHERE `categoryName` = '".$categoryName."'"))){
			echo "<script>alert(\"该类别已经存在！\");location.href=\"index.php?controller=admin&action=addBroadcastCategory\";</script>";	
			exit;
		}else{
			$data = array(
						  	'categoryName'	=>	$categoryName,
							'createtime'	=>	date("Y-m-d H:i")
						  );	
			if($D->insert("broadcastCategory",$data)){
				echo "<script>alert(\"类别添加成功！\");location.href=\"index.php?controller=admin&action=addBroadcastCategory\";</script>";
				exit;
			}else{
				echo "<script>alert(\"类别添加失败！\");location.href=\"index.php?controller=admin&acton=addBroadcastCategory\";</script>";
				exit;
			}
		}
	}else if($action == "ruler"){//系统管理员管理
		include_once("configs/database.inc.php");	
		$D->query("set names utf8");
		$res_admin = $D->fetchAll("admin");
		$smarty->assign('title',"系统管理员");
		$smarty->assign('res_admin',$res_admin);
		$smarty->display('admin/ruler.tpl');
	}else if($action == "addRuler"){//系统管理员列表
		$smarty->assign('title',"添加系统管理员");
		$smarty->display('admin/addRuler.tpl');
	}else if($action == "addRulerAction"){//添加管理员动作
		$username = $_POST['username'];
		$password = $_POST['password'];
		$r_password = $_POST['r_password'];
		if(empty($username)){
			echo "<script>alert(\"用户名不能为空！\");location.href=\"index.php?controller=admin&action=addRuler\";</script>";		
			exit;
		}
		if($password != $r_password){
			echo "<script>alert(\"两次密码不匹配！\");location.href=\"index.php?controller=admin&action=addRuler\";</script>";	
			exit;
		}
		include_once("configs/database.inc.php");		//引入数据库配置
		//检查用户名是否存在
		if($D->rows($D->query("SELECT * FROM admin WHERE username = '".$username."'"))){
		//if($D->rows($D->get_where("admin",array("username" => $username)))){
			echo "<script>alert(\"用户名已经存在！\");location.href=\"index.php?controller=admin&action=addRuler\";</script>";	
			exit;
		}
		$data = array(
					  	"username" 	=>  $username,
						"password"	=>  md5($password),
						"type"		=>	"user",
						"createtime"=>  date("Y-m-d H:i")
					  );
		$D->query("set names utf8");
		if($D->insert("admin",$data)){
				echo "<script>alert(\"管理员添加成功！\");location.href=\"index.php?controller=admin&action=addRuler\";</script>";
		}else{
			echo "<script>alert(\"网络故障，稍后再试！\");location.href=\"index.php?controller=admin&acton=addRuler\";</script>";
		}	

	}else if($action == "updateRuler"){
	/*
	 *	功能：实现更新管理员密码
	 *  日期：2010-04-28
	*/
		$id = $_GET['id'];
		if(empty($id) || !isset($id)){
			header("Location:index.php?controller=admin&action=ruler");	
			exit;
		}
		include_once("configs/database.inc.php");
		$res_ruler_where = $D->get_where("admin",array("id" => $id));
		$res_ruler = $res_ruler_where[0];
		$smarty->assign('res_ruler',$res_ruler);
		$smarty->display('admin/updateRuler.tpl');
	}else if($action == "updateRulerAction"){
	/*
	 *	功能：更新用户密码动作处理
	 *	日期：2010-4-28
	*/
		$id = $_POST['id'];
		$password = $_POST['password'];
		$new_password = $_POST['new_password'];
		if(empty($password) || empty($new_password)){
			header("Location:index.php?controller=admin&action=ruler");	
			exit;
		}
		include("configs/database.inc.php");
		$D->query("set names utf8");
		//检查用户是否存在
		if(!$D->rows($D->query("SELECT * FROM `admin` WHERE `id` = '".$id."' AND `password` = md5( '".$password."' )"))){
			echo "<script>alert(\"用户非法！\");location.href=\"index.php?controller=admin&action=ruler\";</script>";
			exit;
		}
		//进行新密码提交
		$data_array = array("password" => md5($new_password));
		$where_array = array("id" => $id);
		if($D->update("admin",$data_array,$where_array)){
			echo "<script>alert(\"用户密码更新成功！\");location.href=\"index.php?controller=admin&action=ruler\";</script>";
			exit;
			
		}else{
			echo "<script>alert(\"用户密码更新失败！\");location.href=\"index.php?controller=admin&action=ruler\";</script>";
			exit;
		}
	}else if($action == "delRuler"){//删除用户
	/*
	 *	功能：删除用户
	 *	日期：2010-4-27
	*/
		$id = $_GET['id'];
		if(empty($id) || !isset($id)){
			header("Location:index.php?controller=admin&action=ruler");	
			exit;
		}
		include_once("configs/database.inc.php");		//引入数据库配置
		if($D->delete("admin",array("id" => $id))){
			echo "<script>alert(\"删除用户成功！\");location.href=\"index.php?controller=admin&action=ruler\";</script>";
			exit;
		}else{
			echo "<script>alert(\"删除用户失败！\");location.href=\"index.php?controller=admin&action=ruler\";</script>";	
			exit;
		}
	}else if($action == "feedback"){//问题反馈后台
		include_once("configs/database.inc.php");		//引入数据库配置
		$D->query("set names utf8");
		if($D->rowsAll("feedback")){
			$res_feedback = $D->fetchAll("feedback");
		}else{
			$res_feedback = "NULL";	
		}
		//var_dump($res_feedback);
		$smarty->assign('title','意见反馈管理');
		$smarty->assign('res_feedback',$res_feedback);
		$smarty->display('admin/feedback.tpl');
	}else if($action == "showFeedback"){
		$id = $_GET['id'];
		if(empty($id) || !isset($id)){
			header("Location:index.php?controller=admin&action=feedback");	
			exit;
		}
		
		include_once("configs/database.inc.php");		//引入数据库配置
		$D->query("set names utf8");
		$res_feedback_where_sets = $D->get_where("feedback",array("id" => $id));	
		$res_feedback_where = $res_feedback_where_sets[0];
		//print_r($res_feedback_where);
		$smarty->assign('title','反馈详细信息');
		$smarty->assign('res_feedback_where',$res_feedback_where);
		$smarty->display('admin/showFeedback.tpl');
	}else if($action == "delFeedback"){//删除反馈信息
		$id = $_GET['id'];
		if(empty($id) || !isset($id)){
			header("Location:index.php?controller=admin&action=feedback");	
			exit;
		}
		
		include_once("configs/database.inc.php");
		if($D->delete("feedback",array("id" => $id))){
			echo "<script>alert(\"反馈意见删除成功！\");location.href=\"index.php?controller=admin&action=feedback\";</script>";	
			exit;
		}else{
			echo "<script>alert(\"反馈意见删除失败！\");location.href=\"index.php?controller=admin&action=feedback\";</script>";	
		}
	}
?>